//index.js
//获取应用实例
const app = getApp()
// LeanCloud 应用的 ID 和 Key
// AV.init({
//   appId: 'Q7y6ChsRaO66w6DLJ7XR0IF9-gzGzoHsz',
//   appKey: 'tO9g5jhd0ETAx2vrRtR1NLp5',
// });


Page({
    data: {
        userInfo: {},
        music_url: 'http://pcgm4rcvg.bkt.clouddn.com/bgmusic_yudao.mp3',
        isPlayingMusic: true,
        title: '我们要结婚啦',
        coverImgUrl: 'http://pecyxjmxg.bkt.clouddn.com/132-min.JPG',
        testimgUrls: [
            'http://pecyxjmxg.bkt.clouddn.com/009-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/015-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/026-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/031-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/035-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/045-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/062-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/051-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/094-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/105-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/116-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/131-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/133-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/148-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/156-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/177-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/199-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/201-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/204-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/214-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/220-min.JPG',
            'http://pecyxjmxg.bkt.clouddn.com/223-min.JPG'
        ],
    },
    onLoad: function () {
        var that = this
        const backgroundAudioManager = wx.getBackgroundAudioManager()
        player()

        function player() {
            backgroundAudioManager.title = that.data.title,
                backgroundAudioManager.src = that.data.music_url
            backgroundAudioManager.coverImgUrl = that.data.coverImgUrl
            backgroundAudioManager.play();

            backgroundAudioManager.onEnded(() => {
                player()
            })
        }

        // wx.playBackgroundAudio({
        //   dataUrl: this.data.music_url,
        //   title: this.data.title,
        //   coverImgUrl: this.data.coverImgUrl
        // })


    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        // 页面显示
        wx.getBackgroundAudioManager().play();
        this.setData({
            isPlayingMusic: true
        })
    },
    onHide: function () {
        // 页面隐藏
        wx.getBackgroundAudioManager().pause();
        this.setData({
            isPlayingMusic: false
        })
    },

    onUnload: function () {
        // 页面关闭
        wx.getBackgroundAudioManager().pause();
        this.setData({
            isPlayingMusic: false
        })
    },

    onShareAppMessage: function (res) {
        return {
            title: app.globalData.shareData.share,
            imageUrl: app.globalData.shareData.thumb,
            path: 'pages/invitation/index',
            success: function (res) {
                wx.showToast({
                    title: '分享成功',
                })
            },
            fail: function (res) {
                // 转发失败
                wx.showToast({
                    title: '分享取消',
                })
            }
        }
    },

    play: function (event) {
        if (this.data.isPlayingMusic) {
            wx.pauseBackgroundAudio();
            this.setData({
                isPlayingMusic: false
            })
        } else {
            wx.playBackgroundAudio({
                dataUrl: this.data.music_url,
                title: this.data.title,
                coverImgUrl: this.data.coverImgUrl
            })
            this.setData({
                isPlayingMusic: true
            })
        }
    },
})
